package com.nkym.wypan.controller;/*
 * @ClassName RecycleController
 * @Desc TODO
 * @Author 19637
 * @Date 2023/7/7 16:45
 * @Version 1.0
 */

import cn.hutool.core.text.StrSplitter;
import com.nkym.wypan.constants.FileConstants;
import com.nkym.wypan.enums.FileDelFlag;
import com.nkym.wypan.model.context.recycle.RecycleFileDeleteContext;
import com.nkym.wypan.model.context.recycle.RecyclePageContext;
import com.nkym.wypan.model.context.recycle.RestoreContext;
import com.nkym.wypan.model.req.recycle.RecycleFileDeleteReq;
import com.nkym.wypan.model.req.recycle.RecyclePageReq;
import com.nkym.wypan.model.req.recycle.RestoreReq;
import com.nkym.wypan.model.resp.recycle.RecycleListVo;
import com.nkym.wypan.model.resp.recycle.RecyclePageVo;
import com.nkym.wypan.response.BaseResponse;
import com.nkym.wypan.service.RecycleService;
import com.nkym.wypan.utils.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/recycle")
@Api(tags = "回收站模块")
public class RecycleController {
    @Autowired
    RecycleService recycleService;


    @PostMapping("/page-list")
    @ApiOperation(
            value = "回收站文件列表",
            notes = "提供回收站文件列表查询功能")
    public BaseResponse<RecycleListVo> pageList(@RequestBody @Valid RecyclePageReq req) {
        Long userId = UserContextHolder.getId();
        int offset = (req.getPageNow() - 1) * req.getPageSize();
        RecyclePageContext context = new RecyclePageContext();
        context.setSize(req.getPageSize());
        context.setUserId(userId);
        context.setOffset(offset);
        context.setDelFlag(FileDelFlag.RECYCLE.getCode());
        RecycleListVo res = recycleService.pageList(context);
        return BaseResponse.ok(res);
    }

    @PostMapping("/restore")
    @ApiOperation(
            value = "文件恢复",
            notes = "提供文件从回收站恢复操作")
    public BaseResponse restore(@RequestBody @Valid RestoreReq req) {
        String fids = req.getFileIds();
        Long userId = UserContextHolder.getId();
        List<Long> fileIds = StrSplitter.split(fids, FileConstants.SEPARATOR, 0, true, true)
                .stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());
        RestoreContext restoreContext = new RestoreContext();
        restoreContext.setFileIds(fileIds);
        restoreContext.setUserId(userId);
        recycleService.restore(restoreContext);
        return BaseResponse.ok();
    }

    @PostMapping("/delete-file")
    @ApiOperation(
            value = "回收站文件删除 （彻底删除）",
            notes = "提供文件删除功能")
    public BaseResponse deleteFile(@RequestBody @Valid RecycleFileDeleteReq req) {
        String fids = req.getFileIds();
        List<Long> fileIds = StrSplitter.split(fids, FileConstants.SEPARATOR, 0, true, true).stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());
        Long userId = UserContextHolder.getId();

        RecycleFileDeleteContext context = new RecycleFileDeleteContext();
        context.setFileIds(fileIds);
        context.setUserId(userId);
        recycleService.deleteFile(context);
        return BaseResponse.ok();
    }

}
